package com.huawei.esdk.tp.business.common.confctrl;

import java.util.LinkedList;
import java.util.List;

import org.apache.log4j.Logger;

import com.huawei.esdk.platform.common.SDKErrorCode;
import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.config.ConfigManager;
import com.huawei.esdk.platform.common.exception.SDKException;
import com.huawei.esdk.platform.common.utils.StringUtils;
import com.huawei.esdk.tp.base.BaseService;
import com.huawei.esdk.tp.common.ErrInfo;
import com.huawei.esdk.tp.domain.model.Conference;
import com.huawei.esdk.tp.domain.model.HoldingConference;
import com.huawei.esdk.tp.domain.model.Site;
import com.huawei.esdk.tp.domain.model.SiteInfoCommService;
import com.huawei.esdk.tp.domain.model.bean.ConfStatus;

public class ConfCtrlService extends BaseService
{
    private static final Logger LOGGER = Logger.getLogger(ConfCtrlService.class);
    
    private SiteInfoCommService siteInfoCommService;
    
    public SiteInfoCommService getSiteInfoCommService()
    {
        return siteInfoCommService;
    }
    
    public void setSiteInfoCommService(SiteInfoCommService siteInfoCommService)
    {
        this.siteInfoCommService = siteInfoCommService;
    }
    
    public SDKErrorCode endConf(String confid)
        throws SDKException
    {
        SDKErrorCode errorCode = new SDKErrorCode();
        if (StringUtils.isEmpty(confid))
        {
            errorCode.setErrCode(ErrInfo.CONF_ID_ISNULL_ERRORCODE);
            errorCode.setDescription(ErrInfo.CONF_ID_ISNULL_ERRORDESC);
            return errorCode;
        }
        if (!StringUtils.isUintNumber(confid))
        {
            errorCode.setErrCode(ErrInfo.CONF_ID_IS_INVALID_ERRORCODE);
            return errorCode;
        }
        List<String> confIds = new LinkedList<String>();
        confIds.add(confid);
        SDKResult<List<Conference>> status = Conference.queryConferencesStatus(confIds);
        if (null == status.getResult().get(0).getStatus()
            || !ConfStatus.ONGOING.equals(status.getResult().get(0).getStatus()))
        {
            errorCode.setErrCode(ErrInfo.CONF_ISNOTCONVENING_ERRORCODE);
            return errorCode;
        }
        Conference conf = new Conference(confid);
        errorCode = conf.deleteConference();
        
        return errorCode;
    }
    
    public SDKErrorCode addSiteToConf(String confId, String siteUri)
        throws SDKException
    {
        SDKErrorCode result = new SDKErrorCode();
        // 做参数判断
        if (StringUtils.isEmpty(confId))
        {
            LOGGER.debug("confId is null");
            result.setErrCode(ErrInfo.CONF_ID_ISNULL_ERRORCODE);
            return result;
        }
        
        if (!StringUtils.isUintNumber(confId))
        {
            result.setErrCode(ErrInfo.CONF_ID_IS_INVALID_ERRORCODE);
            return result;
        }
        //判断会议是否召开
        List<String> confIds = new LinkedList<String>();
        confIds.add(confId);
        SDKResult<List<Conference>> status = Conference.queryConferencesStatus(confIds);
        if (null == status.getResult().get(0).getStatus()
            || !ConfStatus.ONGOING.equals(status.getResult().get(0).getStatus()))
        {
            result.setErrCode(ErrInfo.CONF_ISNOTCONVENING_ERRORCODE);
            return result;
        }
        
        //准备模型bean
        HoldingConference holdingConference = new HoldingConference(confId);
        
        Site site = siteInfoCommService.getSite(siteUri);
        //会场uri未在SMC中注册，允许添加，会场需要的默认值由SDK来指定，可通过配置文件配置
        if (null == site)
        {
            site = new Site(siteUri);
            site.setDialingMode(ConfigManager.getInstance().getValue("common.scheduleConf.dialingMode"));
            site.setName(ConfigManager.getInstance().getValue("common.addSiteToConfImpl.name"));
            site.setSiteType(ConfigManager.getInstance().getValue("common.addSiteToScheduledConf.SiteType"));
            
            result = holdingConference.addSite(site);
            return result;
        }
        
        site.setDialingMode(ConfigManager.getInstance().getValue("common.scheduleConf.dialingMode"));
        result = holdingConference.addSite(site);
        
        return result;
    }
    
    /**
     * 从已召开的会议中删除会场
     * @throws SDKException 
     */
    public SDKErrorCode delSiteFromConf(String confId, String siteUri)
        throws SDKException
    {
        SDKErrorCode result = new SDKErrorCode();
        if (StringUtils.isEmpty(confId))
        {
            result.setErrCode(ErrInfo.CONF_ID_ISNULL_ERRORCODE);
            return result;
        }
        if (StringUtils.isEmpty(siteUri))
        {
            result.setErrCode(ErrInfo.SITE_URI_ISNULL_ERRORCODE);
            return result;
        }
        if (!StringUtils.isUintNumber(confId))
        {
            result.setErrCode(ErrInfo.CONF_ID_IS_INVALID_ERRORCODE);
            return result;
        }
        
        List<String> confIds = new LinkedList<String>();
        confIds.add(confId);
        SDKResult<List<Conference>> status = Conference.queryConferencesStatus(confIds);
        if (null == status.getResult().get(0).getStatus()
            || !ConfStatus.ONGOING.equals(status.getResult().get(0).getStatus()))
        {
            result.setErrCode(ErrInfo.CONF_ISNOTCONVENING_ERRORCODE);
            return result;
        }
        // 准备Model
        HoldingConference conferencedelSite = new HoldingConference(confId);
        // 调用model
        result = conferencedelSite.delSiteFromConf(siteUri);
        
        return result;
    }
    
    /**
     * 设置已召开的会议的声控转换
     * @throws SDKException 
     */
    public SDKErrorCode setAudioSwitch(String confId, Boolean onOff)
        throws SDKException
    {
        SDKErrorCode result = new SDKErrorCode();
        
        if (StringUtils.isEmpty(confId))
        {
            result.setErrCode(ErrInfo.CONF_ID_ISNULL_ERRORCODE);
            return result;
        }
        if (!StringUtils.isUintNumber(confId))
        {
            result.setErrCode(ErrInfo.CONF_ID_IS_INVALID_ERRORCODE);
            return result;
        }
        
        if (null == onOff)
        {
            result.setErrCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
            return result;
        }
        // 准备Model
        HoldingConference conferenceAudioSwitch = new HoldingConference(confId);
        // 调用model
        result = conferenceAudioSwitch.setAudioSwitch(onOff);
        
        return result;
    }
}
